/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gardenmaker;

import java.awt.geom.Rectangle2D;
import java.awt.TexturePaint;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Ellipse2D.Double;
import static java.lang.Math.*;

/**
 *
 * @author lenny
 */
public class Circulo extends Poligono {
    
    private Double circulo;
    
    public Circulo(int x, int y, String name) {
        super(x, y, name);
        altura= 100;
        largura = 100;

        jprop.setTHeight(altura);
        jprop.setTWidth(largura);
        circulo = new Ellipse2D.Double(x, y, altura, largura);

        formaArea = new Area(circulo);
        selectionSquare();
    }
    
     @Override
    public void setXY(int x, int y) {
         int newX = x-this.x;
         int newY = y-this.y;
        this.x = x;
        this.y = y;
        formaArea.transform(AffineTransform.getTranslateInstance(newX, newY));
        selectionSquare();
    }
     
      @Override
    public void setAltura(int a) {
        if (altura == a) {
            return;
        }
        float c = percentConvert(altura, a);
        int newY = altura - a;
        formaArea.transform(AffineTransform.getTranslateInstance(0, newY));
        formaArea.transform(AffineTransform.getScaleInstance(1, c));
        altura = a;
        selectionSquare();

    }

    @Override
    public void setLargura(int l) {
        
        if(largura==l) return;
        float c = percentConvert(largura, l);
        int newX = largura - l;
        formaArea.transform(AffineTransform.getTranslateInstance(newX, 0));
        formaArea.transform(AffineTransform.getScaleInstance(c, 1));
        largura = l;
        selectionSquare();
    }
    
    @Override
    public void setRotacao(int rotacao) {
        this.rotacao = rotacao;
        double rad = toRadians(rotacao);
        AffineTransform at = AffineTransform.getRotateInstance(rad,
                x + largura, y + altura);
        formaArea.transform(at);
    }


     @Override
    public void desenhar(Graphics g){
        Graphics2D g2 = (Graphics2D) g;
         BufferedImage bi = null;
          if (typeArea == TYPE_AREA_RELVA) {
           try {
                bi = ImageIO.read(new File("./src/grass.jpg"));
            } catch (IOException ex) {
                ex.printStackTrace();
                System.exit(0);
            }
            int height = bi.getHeight();
            int width = bi.getWidth();

            TexturePaint paint = new TexturePaint(bi,
                    new Rectangle2D.Double(0, 0, width, height));

            g2.setPaint(paint);
            g2.fill((Shape) formaArea);
        }

        if (typeArea == TYPE_AREA_PASSEIO) {
             try {
                bi = ImageIO.read(new File("./src/sidewalk.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
                System.exit(0);
            }
            int height = bi.getHeight();
            int width = bi.getWidth();

            TexturePaint paint = new TexturePaint(bi,
                    new Rectangle2D.Double(0, 0, width, height));

            g2.setPaint(paint);
            g2.fill((Shape) formaArea);
        }
        g2.setColor (Color.BLACK);
        g2.draw(formaArea);
    }
     
     @Override
    public void desenharSelBox(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        selectionSquare();
        g2.setColor(Color.GREEN);
        g2.draw(selBox);
        g2.setColor(Color.BLACK);
    }
     
     @Override
    public Area getArea(){
        return formaArea;
    }
     
      @Override
    public void setArea(Area ar){
       this.formaArea=ar; 
    }
    
}
